<template>
  <div class="app-container">
    <el-row style="height: 800px">
      <el-col :span="5" class="app-container"
              style="border: 1px solid #DCDFE6;height: 100%;margin-right: 30px;border-radius: 25px">
        <el-button type="primary" style="margin-bottom: 20px;" @click="NewType = true">新增类别</el-button>
        <el-menu>
          <el-menu-item v-for="item in tableData" @click="queryByIdTimetable(item)">
            <i class="el-icon-s-grid"></i>
            <span slot="title">{{item.timetable_name}}</span>
          </el-menu-item>
        </el-menu>
      </el-col>
      <el-col :span="18" style="border: 1px solid #DCDFE6;height:100%;border-radius: 25px" class="app-container">
          <el-col :span="24">
            <el-button type="primary" style="margin-right: 10px;margin-bottom: 10px;margin-top: 10px" @click="dialog1 = true">新增</el-button>说明：【起始日期】必须是每周一，【结束日期】必须是每周日。
          </el-col>
        <el-dialog title="新增学期" :visible.sync="dialog1" width="300px">
          <el-form ref="addData" :model="addData" :rules="rules" label-width="80px">
            <el-form-item label="学期名称" prop="timetable_name">
              <el-input placeholder="请输入" v-model="addData.timetable_name"></el-input>
            </el-form-item>
            <el-form-item label="起始时间" prop="begin_time">
                <el-time-picker placeholder="选择时间" v-model="addData.begin_time" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;"></el-time-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="end_time">
                <el-time-picker placeholder="选择时间" v-model="addData.end_time" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;"></el-time-picker>
            </el-form-item>
            <el-form-item label="作息时间" prop="timetable_id">
              <el-select value="" placeholder="请选择" v-model="addData.timetable_id">
                <el-option v-for="item in tableData" :label="item.timetable_name" :value="item.unid">{{item.timetable_name}}</el-option>
              </el-select>
            </el-form-item>
            <div style="text-align: center">
              <el-button type="primary" @click="addTimetableMain('addData')">提交</el-button>
            </div>
          </el-form>
        </el-dialog>
        <el-dialog title="新增物品" :visible.sync="AddData" width="500px">
          <el-form label-width="60px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="类别:">
                  <el-select value="">
                    <el-option label="区域一" value="shanghai"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="名称:">
                  <el-select value="">
                    <el-option label="区域一" value="shanghai"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="编码:">
                  <el-input></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="规格:">
                  <el-input></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="单位:">
                  <el-input></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="销售单价:" label-width="80px">
                  <el-input></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="状态:">
                  <el-radio label="1">启用</el-radio>
                  <el-radio label="1">停用</el-radio>
                  <el-checkbox v-model="checked" style="font-weight: bold;">是否允许退费</el-checkbox>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="适用课程:" label-width="80px">
                  <el-select value="">
                    <el-option label="区域一" value="shanghai">教材</el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注:">
                  <el-input></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-dialog>
        <el-dialog title="新增作息时间" :visible.sync="NewType" width="300px">
          <div style="text-align: center">
            <el-row>
              <el-col :span="24">
                <el-form :model="time" ref="time" :rules="rules" label-width="90px">
                  <el-form-item label="时间段名称" label-width="100px" prop="timetable_name2">
                    <el-input v-model="time.timetable_name2"></el-input>
                  </el-form-item>
                </el-form>
              </el-col>
              <el-button type="primary" @click="addTime('time')">提交</el-button>
            </el-row>
          </div>
        </el-dialog>
        <el-table :data="queryData" border style="width: 100%">
          <el-table-column fixed prop="timetable_name" label="名称" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="begin_time" label="开始时间" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="end_time" label="结束时间" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column fixed="right" label="操作" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-button @click="handleClick(scope.row)" type="primary" size="small">查看</el-button>
              <el-button type="warning" size="small">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { queryTimetable,addTimetable,addTimetableMain,queryByIdTimetable } from '@/api/studentRecruit.js'
  export default {
    name: 'index',
    data() {
      return {
        queryName:null,
        queryData:[],
        addData:{
          timetable_name:null,
          begin_time:null,
          end_time:null,
          timetable_id:null,
        },
        queryTableData:null,
        dialog1:false,
        time:{
          timetable_name2:null,
        },
        tableData:null,
        NewType: false,
        checked: false,
        AddData: false,
        tableData2: [{
          id: 1,
          name: '哈哈',
          danwei: '前端'
        }],
        rules: {
          timetable_name: [
            { required: true, message: '请输入学期名称', trigger: 'blur,change' }
          ],
          begin_time: [
            { required: true, message: '请选择起始时间', trigger: 'blur,change' }
          ],
          end_time: [
            { required: true, message: '请选择结束事件', trigger: 'blur,change' }
          ],
          timetable_id: [
            { required: true, message: '请选择作息事件', trigger: 'blur,change' }
          ],
          timetable_name2: [
            { required: true, message: '请输入时间段名称', trigger: 'blur,change' }
          ],
        },
      }
    },
    methods: {
      //获取信息 - 时间表名字(Timetable)主键
      async queryByIdTimetable(item){
        console.log(item)
        let queryData = {
          unid:item.unid
        }
        let resData = await queryByIdTimetable(queryData)
        if(resData.code == 200){
          this.$message.success('查询成功！')
          this.queryName = resData.data
          this.queryData = resData.data[0].main
        }else {
          this.$message.error('查询失败！')
        }
        console.log('resData',resData.data[0])
      },
      handleClick(tab, event) {
        console.log(tab, event)
      },
      //侧边栏所有数据 - 时间表名字(Timetable)列表
      async queryTimetable(){
        let queryData = {

        }
        let resData = await queryTimetable(queryData)
        this.tableData = resData.data
        console.log(resData.data)
      },
      addTime(time){
        this.$refs[time].validate((valid) => {
          if (valid) {
            this.NewType = false
            let addData = {
              timetable_name:this.time.timetable_name2
            }
            addTimetable(addData).then(resData => {
              if (resData.code == 200){
                console.log(resData)
                this.$message.success('添加成功');
                this.queryTimetable()
              }else {
                this.$message.error('添加失败');
              }
            })
          } else {
            console.log(valid);
            return false;
          }
        });
      },
      addTimetableMain(addData){
        this.$refs[addData].validate((valid) => {
          if (valid) {
            let beginTime = new Date(this.addData.begin_time)
            let endTime = new Date(this.addData.end_time)
            if (+endTime <= +beginTime){
              this.$message.error('结束时间必须大于起始时间')
            }else if(+endTime > +beginTime){
              addTimetableMain(this.addData).then(resData => {
                if (resData.code == 200){
                  console.log('resData',resData)
                  this.$message.success('添加成功！')
                }else {
                  this.$message.error('添加失败！')
                }
              })
            }
          } else {
            console.log(valid);
            return false;
          }
        });
      }
    },
    created() {
      this.queryTimetable()
    }
  }
</script>

<style scoped>

</style>
